package Java7_Concurrency_Cookbook.concurrent_collection_framework.atomic_variable;

/**
 * 两个线程同时操作Account.balance不会产生数据不一致问题<br/>
 * Java还提供了其他原子类, AtomicBoolean, AtomicInteger, AtomicReference<br/>
 * @author lixiangke
 * @date 2017/11/24
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        Account account = new Account();
        account.setBalance(1000);

        Company company = new Company(account);
        Thread companyThread = new Thread(company);

        Bank bank = new Bank(account);
        Thread bankThread = new Thread(bank);

        System.out.printf("Account: Initial Balance: %d \n", account.getBalance());

        companyThread.start();
        bankThread.start();
        companyThread.join();
        bankThread.join();
        System.out.printf("Account: Final Balance: %d", account.getBalance());
    }
}
